package com.ruoyi.system.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.system.dto.AliyunSmsDTO;
import com.ruoyi.system.dto.TencentSmsDTO;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import org.springframework.stereotype.Service;

@Service
public class SendSmsUtil {

    //ALI
    private static final String ACCESS_KEY_ID = "LTAI5tP9JXvu8mhy5vQSCGpd";
    private static final String ACCESS_KEY_SECRET = "qT1N8VDD5m5IcWAr9UuiDh5XPDgPo6";
    private static final String ALI_SIGN_NAME = "深圳刑知文化传播";

    //TENCENT
    private static final String SECRET_ID = "AKID5GUk5jPDtPkvvkNp6BAxZx74qQmvtfGl";
    private static final String SECRET_KEY = "NXY558DHLUl3Y0Ur6A1e8TcaZN1Uo260";
    private static final String SDK_APP_ID = "1401009882";
    private static final String TENCENT_SIGN_NAME = "刑知文化";

    public void sendSms(AliyunSmsDTO aliyunSmsDTO) throws ClientException {

        // 初始化客户端
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        // 创建发送短信请求
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(aliyunSmsDTO.getPhoneNumbers());
        request.setSignName(ALI_SIGN_NAME); // 设置短信签名
        request.setTemplateCode(aliyunSmsDTO.getTemplateCode()); // 设置模板 ID
        request.setTemplateParam(aliyunSmsDTO.getTemplateParam()); // 设置模板参数

        // 发送短信
        SendSmsResponse response = client.getAcsResponse(request);
        if ("OK".equals(response.getCode())) {
            System.out.println("发送成功");
        } else {
            System.err.println("发送失败：" + response.getMessage());
        }
    }

    public void sendSms(TencentSmsDTO tencentSmsDTO) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("POST");
        httpProfile.setConnTimeout(60);
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new com.tencentcloudapi.sms.v20210111.SmsClient(cred, "ap-beijing", clientProfile);
        com.tencentcloudapi.sms.v20210111.models.SendSmsRequest req = new com.tencentcloudapi.sms.v20210111.models.SendSmsRequest();
        req.setSmsSdkAppId(SDK_APP_ID);
        req.setSignName(TENCENT_SIGN_NAME);
        req.setTemplateId(tencentSmsDTO.getTemplateId());
        req.setTemplateParamSet(tencentSmsDTO.getTemplateParamSet());
        req.setPhoneNumberSet(tencentSmsDTO.getPhoneNumberSet());
        client.SendSms(req);
    }

}
